package gui;

//import MainFrame1;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.management.relation.Role;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import user.Conference;
import user.User;

import document.Manuscript;
import document.Review;
/**
 * 
 * @author Daniel BRyant
 * @author for backButton actions method Chris Yeaman
 * 
 * some code taken from examples at 
 * http://docs.oracle.com/javase/tutorial/uiswing/examples/components/
 * TextSamplerDemoProject/src/components/TextSamplerDemo.java
 * 
 */
public class PCDecisionPanel extends JPanel
{

	protected static final String titleFieldString = "Title";
	protected static final String authorFieldString = "Author";
	protected static final String dateString = "Current Date";
	protected static final String confString = "Conference";
	protected JLabel actionLabel;
	private MainFrame1 my_parent;

	public PCDecisionPanel(final Manuscript m, final MainFrame1 mf1) {
		my_parent = mf1;
	  setLayout(new BorderLayout());

		//Create a regular text field.
		final JTextField titleField = new JTextField(40);
		titleField.setEditable(false);
		titleField.setText(m.getTitle());

		//Create a password field.
		final JTextField authorField = new JTextField(40);
		authorField.setEditable(false);
		authorField.setText(mf1.getCurrentManuscript().getUser().getFullName());

		//create a conference field
		final JTextField confField = new JTextField(40);
		confField.setEditable(false);
		confField.setText(m.getConference().getConferenceName());

		//Create a date field.
		final JFormattedTextField date = new JFormattedTextField(
				java.util.Calendar.getInstance().getTime());
		date.setEditable(false);

		//Create some labels for the fields.
		JLabel titleFieldLabel = new JLabel(titleFieldString + ": ");
		titleFieldLabel.setLabelFor(titleField);
		JLabel authorFieldLabel = new JLabel(authorFieldString + ": ");
		authorFieldLabel.setLabelFor(authorField);
		JLabel confLabel = new JLabel(confString + ": ");
		confLabel.setLabelFor(confField);
		JLabel dateLabel = new JLabel(dateString + ": ");
		dateLabel.setLabelFor(date);

		//Create a label to put messages during an action event.
		actionLabel = new JLabel();
		actionLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));

		//Lay out the text controls and the labels.
		JPanel textControlsPane = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		textControlsPane.setLayout(gridbag);

		JLabel[] labels = {titleFieldLabel, authorFieldLabel, confLabel, dateLabel};
		JTextField[] textFields = {titleField, authorField, confField, date};
		addLabelTextRows(labels, textFields, gridbag, textControlsPane);

		c.gridwidth = GridBagConstraints.REMAINDER; //last
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 1.0;
		textControlsPane.add(actionLabel, c);
		textControlsPane.setBorder(
				BorderFactory.createCompoundBorder(
						BorderFactory.createTitledBorder("Manuscript Details"),
						BorderFactory.createEmptyBorder(5,5,5,5)));

		//Create a manuscript text area.
		JTextArea textArea = new JTextArea(
				m.getText()
				);
		textArea.setEditable(false);
		textArea.setFont(new Font("Serif", Font.PLAIN, 14));
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		JScrollPane textScrollPane = new JScrollPane(textArea);
		textScrollPane.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		textScrollPane.setPreferredSize(new Dimension(400, 300));
		textScrollPane.setBorder(
				BorderFactory.createCompoundBorder(
						BorderFactory.createCompoundBorder(
								BorderFactory.createTitledBorder("The Manuscript"),
								BorderFactory.createEmptyBorder(5,5,5,5)),
								textScrollPane.getBorder()));

		//Create a review text area.
		JTextArea revArea = new JTextArea(
				reviewstoString(m)
				);
		revArea.setEditable(false);
		revArea.setFont(new Font("Serif", Font.PLAIN, 14));
		revArea.setLineWrap(true);
		revArea.setWrapStyleWord(true);
		JScrollPane revScrollPane = new JScrollPane(revArea);
		revScrollPane.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		revScrollPane.setPreferredSize(new Dimension(400, 500));
		revScrollPane.setBorder(
				BorderFactory.createCompoundBorder(
						BorderFactory.createCompoundBorder(
								BorderFactory.createTitledBorder("The Reviews"),
								BorderFactory.createEmptyBorder(5,5,5,5)),
								revScrollPane.getBorder()));
		
		//create a back button
		//backButton Author Christopher Yeaman
		final JButton backButton = new JButton("Back");

		backButton.addActionListener(new ActionListener() 
		{
			
			public void actionPerformed(ActionEvent e) 
			{
	      my_parent.remove(my_parent.my_panel2);
	      my_parent.my_panel1 = new PCViewsPanel2(my_parent);
	      my_parent.add(my_parent.my_panel1, BorderLayout.CENTER);
	      my_parent.pack();
	      my_parent.refresh();
			}
		});

		//create a accept button
		final JButton yesButton = new JButton("Accept");
		yesButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				JOptionPane.showMessageDialog(yesButton, "Manuscript Accepted");
				m.setStatus(1);
//				System.out.println("you clicked accept, status is now " + m.getStatus());
			}
		});

		//create a reject button
		final JButton noButton = new JButton("Reject");
		noButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				JOptionPane.showMessageDialog(noButton, "Manuscript Rejected");
				m.setStatus(-1);
//				System.out.println("you clicked reject, status is now " + m.getStatus());
			}
		});

		//create a undecided button
		final JButton undecButton = new JButton("Undecided");
		undecButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(undecButton, "Manuscript Undecided");
				m.setStatus(0);
//				System.out.println("you clicked undecided, status is now " + m.getStatus());
			}
		});

		JPanel backPanel = new JPanel();
		GridLayout grid = new GridLayout(1,2);

		backPanel.setLayout(grid);
		backPanel.add(backButton, BorderLayout.EAST);
		// JButton jButton = new JButton();
		//backPanel.add(jButton);


		//make a panel to house the buttons
		JPanel selectorPanel = new JPanel();		
		selectorPanel.setPreferredSize(new Dimension(1000,75));
		selectorPanel.setLayout(new GridLayout(1,3)); 
		selectorPanel.add(yesButton);
		selectorPanel.add(noButton);
		selectorPanel.add(undecButton);

		JPanel rightPane = new JPanel(new BorderLayout());
		rightPane.add(textControlsPane, BorderLayout.PAGE_END);
		rightPane.add(revScrollPane,
				BorderLayout.CENTER);

		//Put everything together.
		JPanel leftPane = new JPanel(new BorderLayout());
		leftPane.add(textControlsPane,
				BorderLayout.PAGE_START);

		leftPane.add(textScrollPane,
				BorderLayout.CENTER);

		add(backPanel, BorderLayout.NORTH);
		add(leftPane, BorderLayout.LINE_START);
		add(rightPane, BorderLayout.LINE_END);
		add(selectorPanel, BorderLayout.SOUTH);

	}

	// METHODS ********************************************

	/**
	 * this method takes an empty string, and for each review r in the manuscripts
	 * set of reviews, it gets the text as a string, and throws it on the end of
	 * the_String, along with a newline
	 * 
	 * @param man a manuscript that is passed into PCDecisionPanel
	 * @return a string, that is just all of the reviews put into one string
	 */
	public String reviewstoString(Manuscript man) {

		StringBuffer s = new StringBuffer();

		for (Review r : man.getAllReviews()) {
			s.append("REVIEW By " + r.getUser().getFirstName() + " " + r.getUser().getLastName() +":" + "\n\n");
			s.append(r.getText() + '\n');
			
		}
		return s.toString();

	}

	private void refresh(final MainFrame1 mf1) {
		mf1.setVisible(true);
		Conference newConference = mf1.getCurrentConference();
		User newUser = mf1.getCurrentUser();
		user.Role newRole = newUser.getRole(newConference);
		
		if (newRole != user.Role.PROGRAM_CHAIR) {
			JOptionPane.showMessageDialog(mf1, "You must be a Program Chair to do that");
			mf1.setVisible(false);
			
		}	
	}
	
	private void addLabelTextRows(JLabel[] labels,
			JTextField[] textFields,
			GridBagLayout gridbag,
			Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		int numLabels = labels.length;

		for (int i = 0; i < numLabels; i++) {
			c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
			c.fill = GridBagConstraints.NONE;      //reset to default
			c.weightx = 0.0;                       //reset to default
			container.add(labels[i], c);

			c.gridwidth = GridBagConstraints.REMAINDER;     //end row
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			container.add(textFields[i], c);
		}
	}

	/**
	 * Create the GUI and show it.  For thread safety,
	 * this method should be invoked from the
	 * event dispatch thread.
	 * @param m 
	 */
	private static void createAndShowGUI(Manuscript m, MainFrame1 mf1) {
		//Create and set up the window.
		JFrame frame = new JFrame("PC Makes Decision");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Add content to the window.
		frame.add(new PCDecisionPanel(m,mf1));

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}


}
